/*
 * @Descripttion: 消息提醒
 * @version: V1.0
 * @Author: GW
 * @Date: 2021-07-29 10:40:42
 * @LastEditors: GW
 * @LastEditTime: 2022-01-19 09:09:51
 */
import { list as listCallSettings} from "@/api/crm/call/callSettings";
import { pageList as voicePageList } from "@/api/crm/call/callVoice";
import {  getMyCrmCallAgent } from "@/api/crm/call/agentManage";
import { isCallEnable} from "@/api/crm/call/callMinutes";
import { call } from "@/api/crm/call/callTaskRecord";

const callingBox = {
    state: {
        devicename: null,           //设备名称 
        voiceSelectData:[],     //音频集合
        voiceMap:new Map(),     //音频Map存储
        settings:{},             //设置
        XYLoginInfo:{},              //祥云智通 坐席账户
        
    },
    getters: {

    },
    mutations: {
        SET_Device_Name (state, newData) {
            state.devicename = newData;
        },
        SET_VOICE_LIST (state, newData) {
            state.voiceSelectData = newData;
        },
        SET_VOICE_MAP (state, newData) {
            state.voiceMap = newData;
        },
        SET_CALL_SETTINGS (state, newData) {
            state.settings = newData;
        },
        SET_XY_LOGININFO(state, newData){
            state.XYLoginInfo = newData;
        }

    },
    actions: {
        //获取 音频列表
        ListVoicePage ({ commit }) {
            return new Promise((resolve, reject) => {
                let voicePostData = {
                    page:{
                       size:-1
                    }
                }
                voicePageList(voicePostData).then(res => {
                    if (res.code === 200) {
                        const { data } = res;
                        const { records} = data;
                        let voiceMap = new Map();
                        let voiceSelectData = [];
                        records.forEach(item=>{
                            voiceMap.set(item.id,item);
                            voiceSelectData.push({
                                key:item.id,
                                title:item.voiceName
                            });
                        });
                        commit('SET_VOICE_LIST', voiceSelectData);
                        commit('SET_VOICE_MAP', voiceMap);
                        console.log("store_获取音频",voiceSelectData);
                        // resolve();
                    }
                }).catch(error => {
                    reject(error);
                });
            })
        },
        //获取 音频设置
        ListCallSettings ({ commit }) {
            return new Promise((resolve, reject) => {
                //基础设置
                const baseSettingsList = [
                    {
                        className:"音量设置",       //分类
                        code:"localaudio",
                        value:"50",
                        dataType: 1,        //数据类型 1：数字 2：字符串 3.Boolean
                        showName:"本地听到音量"
                    },
                    {
                        className:"音量设置",       //分类
                        code:"phoneaudio",
                        value:"30",
                        dataType: 1,        //数据类型 1：数字 2：字符串 3.Boolean
                        showName:"对方听到音量"
                    },
                    {
                        className:"换卡设置",       //分类
                        code:"isAutoChangeSIM",
                        value:"true",
                        dataType: 3,        //数据类型 1：数字 2：字符串 3.Boolean
                        showName:"是否自动换卡"
                    },
                    {
                        className:"换卡设置",       //分类
                        code:"maxCallTimes",
                        value:"5",
                        dataType: 1,        //数据类型 1：数字 2：字符串 3.Boolean
                        showName:"换卡频率"
                    },
                    {
                        className:"外呼基础设置",       //分类
                        code:"callWay",
                        value:"line",
                        dataType: 2,        //数据类型 1：数字 2：字符串 3.Boolean
                        showName:"外呼方式"
                    },
                ];
                listCallSettings().then(res => {
                    if (res.code === 200) {
                        let settings = {};
                        // this.settingsMap = new Map();
                        let newSettingsMap = new Map();
                        let { data } = res;
                        data && data.forEach(item=>item.code && newSettingsMap.set(item.code,item));
                        let settingsList =JSON.parse(JSON.stringify(baseSettingsList));
                        settingsList.forEach(item=>{
                            let setting = newSettingsMap.get(item.code);
                            // console.log("查找设置",setting);
                            if(setting){
                                Object.assign(item,{...setting});
                            }
                            // this.settingsMap.set(item.code,item);
                            let value = item.value;
                            if(item.dataType == 1){
                                value = Number(value);
                            }else if(item.dataType == 3){
                                value = Boolean(value == "true");
                            }
                            settings[item.code] = value;
                        });
                        console.log("外呼设置参数重组完成：",settings);
                        commit('SET_CALL_SETTINGS', settings);
                        // resolve();
                    }
                }).catch(error => {
                    reject(error);
                });
            })
        },
        //设置 设备名称
        SetDeviceName({ commit },devicename) {
            commit('SET_Device_Name', devicename);
        },
        //获取祥云 坐席账户
        GetXYLoginInfo({ commit }){
            return new Promise((resolve, reject) => {
                getMyCrmCallAgent().then(response => {
                    if (response.code === 200) {
                        if(response.data){
                            commit('SET_XY_LOGININFO', response.data);
                            console.log("store_存放坐席数据",response.data);
                            resolve();
                        }
                    };
                    
                }).catch(error => {
                    reject(error);
                });
            })
           
        },
        //是否可以线路外呼
        IsCallEnable({ commit }){
            return new Promise((resolve, reject) => {
                isCallEnable().then(response => {
                    if (response.code === 200) {
                        resolve(response.data);
                    };
                }).catch(error => {
                    reject(error);
                });
            })
           
        },
        //呼出
        Call({ commit },callMode){
            return new Promise((resolve, reject) => {
                call(callMode).then(response => {
                    resolve(response);
                }).catch(error => {
                    reject(error);
                });
            })
           
        },
    },

}
export default callingBox;